package com.newbingo.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.newbingo.dao.imple.IAgentDao;
import com.newbingo.dao.imple.IBettingCellDao;

import com.newbingo.dto.imple.IAgentDto;
import com.newbingo.dto.imple.IBetSettingDto;

import com.newbingo.entity.Agent;
import com.newbingo.entity.BetSetting;
import com.newbingo.entity.BettingCell;

import com.newbingo.service.imple.IAgentService;
import com.newbingo.util.Pager;

public class AgentService implements IAgentService {
	private IAgentDao agentDao = null;
	private IBettingCellDao bettingCellDao = null;
	private Agent agent;
	private IBetSettingDto betsettingDto = null;
	private IAgentDto agentDto;

	/*
	 * 根据agentType查询出支下的代理或股东，并分页显示
	 */
	public Pager findSubAgents(int start, int limit, Integer agentId,
			Integer agentType) {
		return agentDto.findSubAgents(start, limit, agentId, agentType);
		// return agentDao.findSubAgents(start, limit, agentId, agentType);
	}

	public void attachDirty(Agent transientInstance) {
		agentDao.attachDirty(transientInstance);
	}

	public void delete(Agent transientInstance) {
		agentDao.delete(transientInstance);
	}

	public List findAll() {
		return agentDao.findAll();
	}

	public List findByExample(Agent instance) {
		return agentDao.findByExample(instance);
	}

	public Agent findById(Integer id) {
		return agentDao.findById(id);
	}

	public List findByName(Object name) {
		return agentDao.findByName(name);
	}

	public Pager findByPage(int start, int limit) {
		return agentDao.findByPage(start, limit);
	}

	public List findByProperty(String propertyName, Object value) {
		return agentDao.findByProperty(propertyName, value);
	}

	public List findByStatus(Object status) {
		return agentDao.findByStatus(status);
	}

	public Agent merge(Agent transientInstance) {
		return agentDao.merge(transientInstance);
	}

	public void save(Agent transientInstance) {
		// 获取ParentAgent对象
		Agent parent = this.findById(transientInstance.getUno());
		// 设置Set<BetSetting>对象
		transientInstance.setParentAgent(parent);
		Set<BetSetting> betSettings = new HashSet<BetSetting>();
		List<BettingCell> bettingCells = bettingCellDao.findAll();
		for (BettingCell bc : bettingCells) {
			BetSetting bs = new BetSetting();
			bs.setBettingCell(bc);
			bs.setAgent(transientInstance);
			bs.setMaxBet(1000);
			bs.setMinBet(0);
			betSettings.add(bs);
		}
		transientInstance.setSettings(betSettings);
		agentDao.save(transientInstance);

	}

	/*
	 * 修改投注设置
	 */
	public void mergeSetting(BetSetting set) {
		Agent agent = this.findById(set.getAgent().getUno());
		Set<BetSetting> settings = agent.getSettings();
		BetSetting temp = new BetSetting();
		if (settings != null && settings.size() > 0) {
			for (BetSetting bs : settings) {
				if (bs.equals(set))
					temp = bs;
			}
		}
		settings.remove(temp);
		settings.add(set);
	}

	public Pager findBetSetting(Integer agentId, Integer gamePlayId) {
		System.out.println("两个传值的编号是：" + agentId + "  " + gamePlayId);
		return betsettingDto.findBetSettingByPlayIdAndAgentId(agentId,
				gamePlayId);
	}

	// public BetSettingDto getBetSettingDtoObjectById(Integer agentId,Integer
	// gamePlayId,Integer cellId){
	// return betsettingDto.getBetSettingDtoObjectById(agentId, gamePlayId,
	// cellId);
	// }

	public IAgentDao getAgentDao() {
		return agentDao;
	}

	public void setAgentDao(IAgentDao agentDao) {
		this.agentDao = agentDao;
	}

	public Agent findByParentId(Integer parentId) {
		return agentDao.findById(parentId);
	}

	public IBettingCellDao getBettingCellDao() {
		return bettingCellDao;
	}

	public void setBettingCellDao(IBettingCellDao bettingCellDao) {
		this.bettingCellDao = bettingCellDao;
	}

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public IBetSettingDto getBetsettingDto() {
		return betsettingDto;
	}

	public void setBetsettingDto(IBetSettingDto betsettingDto) {
		this.betsettingDto = betsettingDto;
	}

	public IAgentDto getAgentDto() {
		return agentDto;
	}

	public void setAgentDto(IAgentDto agentDto) {
		this.agentDto = agentDto;
	}


}
